package com.arvin.imagepicker.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.arvin.imagepicker.GlideImgLoader;
import com.arvin.imagepicker.ImgLoader;
import com.arvin.imagepicker.bean.ImageItem;
import com.arvin.imagepicker.widget.TouchImageView;


/**
 * Created by jp on 2018/2/1.
 */

public class SinglePreviewFragment extends Fragment {
    public static final String KEY_URL = "key_url";
    private TouchImageView imageView;
    private String url;
    private Context mContext;
    private boolean enableSingleTap = true;//singleTap to do something
    ImgLoader mImagePresenter;//interface to load image,you can implements it with your own code
//    GlideImgLoader mGlideImgLoader;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mContext=getContext();
        mImagePresenter = new GlideImgLoader();

//        mGlideImgLoader=new GlideImgLoader();
        ImageItem imageItem = (ImageItem) bundle.getSerializable(KEY_URL);

        url = imageItem.path;

//        Log.i(TAG, "=====current show image path:" + url);

        imageView = new TouchImageView(getContext());
        imageView.setBackgroundColor(0xff000000);
        DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(params);

        imageView.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if (enableSingleTap) {
                    if(mContext instanceof ImagePreviewFragment.OnImageSingleTapClickListener){
                        ((ImagePreviewFragment.OnImageSingleTapClickListener)mContext).onImageSingleTap(e);
                    }
                }
                return false;
            }
            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                return false;
            }
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                return false;
            }

        });

//        ((UilImgLoader)mImagePresenter).onPresentImage2(imageView, url, imageView.getWidth());//display the image with your own ImageLoader
        mImagePresenter.onPresentImage(imageView,url,displayMetrics.widthPixels);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return imageView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
